using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace Acme.Northwind.Install
{
	/// <summary>
	/// 
	/// </summary>
	public class InstallSettings
	{
		/// <summary>
		/// 
		/// </summary>
		public string PrimaryServer { get; set; }

		/// <summary>
		/// 
		/// </summary>
		public string PrimaryDatabase { get; set; }

		/// <summary>
		/// 
		/// </summary>
		public string PrimaryUserName { get; set; }

		/// <summary>
		/// 
		/// </summary>
		public string PrimaryPassword { get; set; }
		
		/// <summary>
		/// 
		/// </summary>
		public bool PrimaryUseIntegratedSecurity { get; set; }

		/// <summary>
		/// 
		/// </summary>
		public string CloudServer { get; set; }

		/// <summary>
		/// 
		/// </summary>
		public string CloudDatabase { get; set; }

		/// <summary>
		/// 
		/// </summary>
		public string CloudUserName { get; set; }

		/// <summary>
		/// 
		/// </summary>
		public string CloudPassword { get; set; }

		/// <summary>
		/// 
		/// </summary>
		public InstallSettings()
		{
			this.IsLoaded = false;
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsLoaded { get; private set; }

		/// <summary>
		/// 
		/// </summary>
		public bool Load()
		{
			FileInfo fi = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
			fi = new FileInfo(Path.Combine(fi.DirectoryName, "installsettings.xml"));
			if (!fi.Exists) return false;

			XmlDocument document = new XmlDocument();
			document.Load(fi.FullName);

			if (document.DocumentElement.Name == "a")
			{
				this.PrimaryServer = XmlHelper.GetNodeValue(document.DocumentElement, "server", string.Empty);
				this.PrimaryUseIntegratedSecurity = XmlHelper.GetNodeValue(document.DocumentElement, "useintegratedsecurity", false);
				this.PrimaryUserName = XmlHelper.GetNodeValue(document.DocumentElement, "username", string.Empty);
				this.PrimaryPassword = XmlHelper.GetNodeValue(document.DocumentElement, "password", string.Empty);
				this.PrimaryDatabase = XmlHelper.GetNodeValue(document.DocumentElement, "database", string.Empty);
			}
			else
			{
				var node = document.DocumentElement.SelectSingleNode("primary");
				this.PrimaryServer = XmlHelper.GetNodeValue(node, "server", string.Empty);
				this.PrimaryUseIntegratedSecurity = XmlHelper.GetNodeValue(node, "useintegratedsecurity", false);
				this.PrimaryUserName = XmlHelper.GetNodeValue(node, "username", string.Empty);
				this.PrimaryPassword = XmlHelper.GetNodeValue(node, "password", string.Empty);
				this.PrimaryDatabase = XmlHelper.GetNodeValue(node, "database", string.Empty);

				node = document.DocumentElement.SelectSingleNode("cloud");
				this.CloudServer = XmlHelper.GetNodeValue(node, "server", string.Empty);
				this.CloudUserName = XmlHelper.GetNodeValue(node, "username", string.Empty);
				this.CloudPassword = XmlHelper.GetNodeValue(node, "password", string.Empty);
				this.CloudDatabase = XmlHelper.GetNodeValue(node, "database", string.Empty);
			}

			this.IsLoaded = true;
			return true;

		}

		public void Kill()
		{
			FileInfo fi = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
			fi = new FileInfo(Path.Combine(fi.DirectoryName, "installsettings.xml"));
			if (fi.Exists) fi.Delete();
			System.Threading.Thread.Sleep(500);
		}

		/// <summary>
		/// 
		/// </summary>
		public bool Save()
		{
			FileInfo fi = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
			fi = new FileInfo(Path.Combine(fi.DirectoryName, "installsettings.xml"));
			if (fi.Exists) fi.Delete();
			System.Threading.Thread.Sleep(500);

			XmlDocument document = new XmlDocument();
			document.LoadXml("<settings></settings>");

			var node = XmlHelper.AddElement(document.DocumentElement, "primary", string.Empty) as XmlElement;
			XmlHelper.AddElement(node, "server", this.PrimaryServer);
			XmlHelper.AddElement(node, "useintegratedsecurity", this.PrimaryUseIntegratedSecurity.ToString().ToLower());
			XmlHelper.AddElement(node, "username", this.PrimaryUserName);
			XmlHelper.AddElement(node, "password", this.PrimaryPassword);
			XmlHelper.AddElement(node, "database", this.PrimaryDatabase);

			node = XmlHelper.AddElement(document.DocumentElement, "cloud", string.Empty) as XmlElement;
			XmlHelper.AddElement(node, "server", this.CloudServer);
			XmlHelper.AddElement(node, "username", this.CloudUserName);
			XmlHelper.AddElement(node, "password", this.CloudPassword);
			XmlHelper.AddElement(node, "database", this.CloudDatabase);

			document.Save(fi.FullName);

			return true;

		}

		/// <summary>
		/// 
		/// </summary>
		public string GetPrimaryConnectionString()
		{
			if (this.PrimaryUseIntegratedSecurity)
			{
				return "server=" + this.PrimaryServer + ";Initial Catalog=" + this.PrimaryDatabase + ";integrated Security=SSPI;";
			}
			else
			{
				return "server=" + this.PrimaryServer + ";Initial Catalog=" + this.PrimaryDatabase + ";user id=" + this.PrimaryUserName + ";password=" + this.PrimaryPassword + ";";
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string GetCloudConnectionString()
		{
			return "server=" + this.CloudServer + ";Initial Catalog=" + this.CloudDatabase + ";user id=" + this.CloudUserName + ";password=" + this.CloudPassword + ";";
		}

	}
}
